//
//  HJApplyListCell.m
//  KaiChat
//
//  Created by lx on 2023/10/21.
//

#import "HJApplyListCell.h"
#import "QMUIButton.h"

@interface HJApplyListCell()

@property (nonatomic, weak) UIImageView *avatarImageView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *dataLabel;
@property (nonatomic, weak) QMUIButton *waitingButton;
@property (nonatomic, weak) QMUIButton *helloButton;
@property (nonatomic, weak) UILabel *expireLabel;
@property (nonatomic, weak) UIImageView *arrowImageView;

@end

@implementation HJApplyListCell

- (void)settingSubView {
    [super settingSubView];
    
    
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIImageView *avatarImageView = [UIImageView addToView:self.contentView];
    avatarImageView.addCornerRadius(20);
    avatarImageView.contentMode = UIViewContentModeScaleAspectFill;
    [avatarImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(leftRightMargin);
        make.centerY.offset(0);
        make.width.height.offset(40);
    }];
 
    UILabel *nameLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMediumMake(14) color:UIColor.tj_333333Color title:nil textAlignment:NSTextAlignmentLeft];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(avatarImageView.mas_right).offset(10);
        make.top.equalTo(avatarImageView).inset(3);
        make.right.mas_lessThanOrEqualTo(-100);
    }];
    
    UILabel *dataLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:nil textAlignment:NSTextAlignmentLeft];
    [dataLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(nameLabel);
        make.bottom.equalTo(avatarImageView).inset(3);
    }];
    
    UIImageView *arrowImageView = [UIImageView addToView:self.contentView];
    [arrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(dataLabel.mas_right).offset(5);
        make.centerY.equalTo(dataLabel);
    }];
    
    QMUIButton *waitingButton = [QMUIButton buttonWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.whiteColor title:@""];
    waitingButton.addCornerRadius(5);
    waitingButton.hidden = YES;
    waitingButton.backgroundColor = UIColor.tj_mainColor;
    [waitingButton addTarget:self action:@selector(didClickWaitingButton) forControlEvents:UIControlEventTouchUpInside];
    [waitingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.inset(leftRightMargin);
        make.centerY.offset(0);
        make.width.offset(72);
        make.height.offset(30);
    }];
    
    QMUIButton *helloButton = [QMUIButton buttonWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_1D0061Color title:@"Hello"];
    helloButton.addCornerRadius(5);
    helloButton.hidden = YES;
    [helloButton setBackgroundColor:UIColor.tj_F6F3FFColor];
    [helloButton setSpacingBetweenImageAndTitle:6];
    [helloButton setImagePosition:QMUIButtonImagePositionLeft];
    [helloButton setImage:[UIImage imageNamed:@"hello"] forState:UIControlStateNormal];
    [helloButton addTarget:self action:@selector(didClickHelloButton) forControlEvents:UIControlEventTouchUpInside];
    [helloButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.centerY.width.height.equalTo(waitingButton);
    }];
    
    UILabel *expireLabel = [UILabel lableWithSuperView:self.contentView font:UIFontMake(12) color:UIColor.tj_BEBDCAColor title:@"Expire" textAlignment:NSTextAlignmentCenter];
    expireLabel.hidden = YES;
    [expireLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.centerY.width.height.equalTo(waitingButton);
        make.width.offset(72);
        make.height.offset(30);
    }];
    
    _waitingButton = waitingButton;
    _helloButton = helloButton;
    _expireLabel = expireLabel;
    _avatarImageView = avatarImageView;
    _nameLabel = nameLabel;
    _dataLabel = dataLabel;
    _arrowImageView = arrowImageView;
}

- (void)didClickHelloButton {
    if (_didClickHelloButtonBlock) {
        _didClickHelloButtonBlock();
    }
}

- (void)didClickWaitingButton {
    if (_didClickWaitingButtonBlock) {
        _didClickWaitingButtonBlock();
    }
}

- (void)setModel:(HJUserApplyModel *)model {
    _model = model;
    
    _arrowImageView.image = [UIImage imageNamed:_model.way.intValue == 1 ? @"send_arrow" : @"receive_arrow"];
    [_avatarImageView tj_setImageWithUrl:model.avatar placeholderImage:[UIImage imageNamed:defaultAvatarName]];
    _nameLabel.text = model.nickname;
    _dataLabel.text = [[NSDate dateWithTimeIntervalSince1970:model.date.integerValue] stringWithFormat:@"dd MMM yyyy HH:mm"];
    
    NSInteger status = model.status.intValue;
    _waitingButton.userInteractionEnabled = YES;

    if (status == 1) {
        _helloButton.hidden = NO;
        _waitingButton.hidden = YES;
        _expireLabel.hidden = YES;
    } else if (status == 2) {
        _helloButton.hidden = YES;
        _waitingButton.hidden = NO;
        _expireLabel.hidden = YES;
        
        if (model.way.intValue == 1) {//自己发起的
            [_waitingButton setTitle:@"Waiting" forState:UIControlStateNormal];
            [_waitingButton setTitleColor:[UIColor.whiteColor colorWithAlphaComponent:0.3] forState:UIControlStateNormal];
            _waitingButton.userInteractionEnabled = NO;
        } else {
            [_waitingButton setTitle:@"Accept" forState:UIControlStateNormal];
            [_waitingButton setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        }
    } else if (status == 3) {
        _helloButton.hidden = YES;
        _waitingButton.hidden = YES;
        _expireLabel.hidden = NO;
    } else {
        _helloButton.hidden = YES;
        _waitingButton.hidden = YES;
        _expireLabel.hidden = YES;
    }
}



@end
